package com.insigma.business.qrcode.merchant.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.insigma.business.common.dto.CurrentUser;
import com.insigma.business.common.utils.CurrentUserUtil;
import com.insigma.business.common.utils.ExcelUtils;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantDiscountProviderConverter;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantEntryApplyProviderConverter;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantInfoProviderConverter;
import com.insigma.business.qrcode.common.dto.CommonFavoriteDO;
import com.insigma.business.qrcode.common.service.CommonFavoriteService;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.ProcessStatusEnum;
import com.insigma.business.qrcode.common.vo.UserTypeEnum;
import com.insigma.business.qrcode.merchant.converter.CareWelfareMerchantInfoLogProviderConverter;
import com.insigma.business.qrcode.merchant.dao.CareWelfareMerchantDiscountDao;
import com.insigma.business.qrcode.merchant.dao.CareWelfareMerchantEntryApplyDao;
import com.insigma.business.qrcode.merchant.dao.CareWelfareMerchantInfoLogDao;
import com.insigma.business.qrcode.merchant.dto.CareWelfareMerchantEntryApplyDTO;
import com.insigma.business.qrcode.merchant.dto.CareWelfareMerchantEntryApplyExportVO;
import com.insigma.business.qrcode.merchant.dto.CareWelfareMerchantInfoLogDTO;
import com.insigma.business.qrcode.merchant.dto.OutStatusEnum;
import com.insigma.business.qrcode.merchant.dto.request.*;
import com.insigma.business.qrcode.merchant.dto.search.CareWelfareMerchantEntryApplyBSearcher;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantDiscountDO;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantEntryApply;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantInfoLog;
import com.insigma.business.qrcode.merchant.service.CareWelfareMerchantDiscountService;
import com.insigma.business.qrcode.merchant.service.MerchantEnterService;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.merchant.dao.CareWelfareMerchantInfoDao;
import com.insigma.business.qrcode.merchant.dto.search.CareWelfareMerchantInfoBSearcher;
import com.insigma.business.qrcode.welfare.dto.BusinessLicTypeEnum;
import com.insigma.business.qrcode.welfare.dto.CareWelfareMerchantDiscountDTO;
import com.insigma.business.qrcode.welfare.dto.CareWelfareMerchantInfoDTO;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantInfoDO;
import com.insigma.business.qrcode.welfare.dto.MerchantTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MerchantEnterServiceImpl extends BaseProviderImpl implements MerchantEnterService {
    @Resource
    private CareWelfareMerchantEntryApplyDao careWelfareMerchantEntryApplyDao;

    @Resource
    private CareWelfareMerchantInfoDao careWelfareMerchantInfoDao;

    @Resource
    private CareWelfareMerchantInfoLogDao careWelfareMerchantInfoLogDao;

    @Resource
    private CommonFavoriteService commonFavoriteService;

    @Resource
    private CareWelfareMerchantDiscountDao careWelfareMerchantDiscountDao;

    @Resource
    private CareWelfareMerchantDiscountService careWelfareMerchantDiscountService;

    @Override
    public BaseResponse<Integer> enterApply(CareWelfareMerchantEntryApplyInsertRequest request) {
        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(request, "新增的CareWelfareMerchantEntryApply请求参数不能为空");
            }

            @Override
            @Transactional(rollbackFor = Exception.class)
            public void executeService() {
                List<CareWelfareMerchantEntryApply> list = careWelfareMerchantEntryApplyDao.getByUscc(request.getUscc(),"WAIT");
                BizCheckUtil.checkIsTrue(CollectionUtils.isEmpty(list), "该商家已申请入驻！");
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                CareWelfareMerchantEntryApply insert = CareWelfareMerchantEntryApplyProviderConverter.convertRequestIO2DO(request);
                insert.setWeight(1);
                insert.setStatus(ProcessStatusEnum.WAIT.name());
                insert.setPublished(false);
                insert.setDeleted(false);
                insert.setIsAgree(false);
                insert.setOuterSysAccountId(currentUser.getWechatOpenid());
                insert.setCreateDatetime(new Date());
                insert.setCreatorId(CurrentUserUtil.getCurrentUser().getSoldierId());
                insert.setCreatorType(UserTypeEnum.SOLDIER.name());
                insert.setModifyDatetime(new Date());
                insert.setModifierId(CurrentUserUtil.getCurrentUser().getSoldierId());
                insert.setModifierType(UserTypeEnum.SOLDIER.name());
                insert.setSoldierId(StringUtils.isBlank(currentUser.getSoldierId()) ? currentUser.getWechatOpenid() :  currentUser.getSoldierId() );
                int affectRowNum = careWelfareMerchantEntryApplyDao.insert(insert);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });


        return baseResponse;

    }

    @Override
    public BasePagingResponse<List<CareWelfareMerchantEntryApplyDTO>> getApplyList(CareWelfareMerchantEntryApplySearchRequest searchRequest) {
        final BasePagingResponse<List<CareWelfareMerchantEntryApplyDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareMerchantEntryApply的请求参数不能为空");
            }

            @Override
            public void executeService() {
//                PageHelper.startPage(searchRequest.getPageNo(),searchRequest.getPageSize());
                CareWelfareMerchantEntryApplyBSearcher careWelfareMerchantEntryApplyBSearcher = CareWelfareMerchantEntryApplyProviderConverter.convertRequestSO2Searcher(searchRequest);
                PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());
                List<CareWelfareMerchantEntryApply> list = careWelfareMerchantEntryApplyDao.search(careWelfareMerchantEntryApplyBSearcher);
                PageInfo<CareWelfareMerchantEntryApply> pageInfo = new PageInfo<>(list);
                final List<CareWelfareMerchantEntryApplyDTO> careWelfareMerchantInfoDTOList = CareWelfareMerchantEntryApplyProviderConverter.convertResponseDOList2DTOList(list);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, careWelfareMerchantInfoDTOList, searchRequest, pageInfo.getTotal());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_SEARCH;
            }
        });

        return basePagingResponse;

    }

    @Override
    public BaseResponse<CareWelfareMerchantEntryApplyDTO> getApplyDetail(BaseGetRequest baseGetRequest) {
        CareWelfareMerchantEntryApply careWelfareMerchantEntryApply = careWelfareMerchantEntryApplyDao.selectByPrimaryKey((String) baseGetRequest.getId());
        CareWelfareMerchantEntryApplyDTO careWelfareMerchantEntryApplyDTO = CareWelfareMerchantEntryApplyProviderConverter.convertResponseDO2DTO(careWelfareMerchantEntryApply);
        return ResponseUtils.buildSuccessResponse(careWelfareMerchantEntryApplyDTO);
    }

    @Override
    public BaseResponse<Integer> enterAudit(CareWelfareMerchantEntryApplyUpdateRequest updateRequest) {
        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改的CareWelfareMerchantEntryApply请求参数不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getId(), "修改CareWelfareMerchantEntryApply的请求参数ID不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getStatus(), "修改CareWelfareMerchantEntryApply的请求参数STATUS不能为空");
                BizCheckUtil.checkIsTrue("SUCCESS,REJECT".contains(updateRequest.getStatus()),"提交状态不在范围内！");
            }

            @Override
            @Transactional(rollbackFor = Exception.class)
            public void executeService() {
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                CareWelfareMerchantEntryApply old = careWelfareMerchantEntryApplyDao.selectByPrimaryKey(updateRequest.getId());
                CareWelfareMerchantInfoDO usscInfo = careWelfareMerchantInfoDao.getByUscc(updateRequest.getUscc());
                if (ObjectUtil.isNotEmpty(usscInfo) && "IN,OUT_WAIT".contains(usscInfo.getOutStatus())) {
                    BizCheckUtil.checkIsNull(usscInfo, "已存在统一社会信用代码："+updateRequest.getUscc()+"的数据");
                }
                BizCheckUtil.checkNotNull(old, "未找到对应数据");
                BeanUtils.copyProperties(updateRequest,old);
                old.setCreatorId(currentUser.getUserId());
                old.setModifierId(currentUser.getUserId());
                old.setModifyDatetime(new Date());
                old.setModifierType(updateRequest.getModifierType());

                if ("SUCCESS".equals(updateRequest.getStatus())) {
                    // 新增到结果表
                    CareWelfareMerchantInfoDO careWelfareMerchantInfoDO = new CareWelfareMerchantInfoDO();
                    BeanUtils.copyProperties(old,careWelfareMerchantInfoDO);
                    careWelfareMerchantInfoDO.setPublished(old.getPublished());
                    if (old.getPublished()) {
                        careWelfareMerchantInfoDO.setPublishTime(new Date());
                    }
                    careWelfareMerchantInfoDO.setModifierId(currentUser.getUserId());
                    careWelfareMerchantInfoDO.setModifierType(updateRequest.getModifierType());
                    careWelfareMerchantInfoDO.setModifyDatetime(new Date());
                    careWelfareMerchantInfoDO.setEntryId(old.getId());
                    careWelfareMerchantInfoDO.setQrCode(StringUtils.isBlank(careWelfareMerchantInfoDO.getQrCode())?old.getId():careWelfareMerchantInfoDO.getQrCode());
                    careWelfareMerchantInfoDO.setOutStatus(OutStatusEnum.IN.name());
                    careWelfareMerchantInfoDO.setShow(true);
                    if (ObjectUtil.isNotEmpty(usscInfo)) {
                        careWelfareMerchantInfoDO.setCreatorId(usscInfo.getCreatorId());
                        careWelfareMerchantInfoDO.setCreateDatetime(usscInfo.getCreateDatetime());
                        careWelfareMerchantInfoDO.setId(usscInfo.getId());
                        careWelfareMerchantInfoDao.update(careWelfareMerchantInfoDO);
                    } else {
                        careWelfareMerchantInfoDO.setCreatorId(currentUser.getUserId());
                        careWelfareMerchantInfoDO.setCreateDatetime(new Date());
                        careWelfareMerchantInfoDao.insert(careWelfareMerchantInfoDO);
                    }
                }
                int affectRowNum = careWelfareMerchantEntryApplyDao.updateByPrimaryKey(old);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });


        return baseResponse;
    }

    @Override
    public BasePagingResponse<List<CareWelfareMerchantInfoDTO>> managerSearch(CareWelfareMerchantInfoSearchRequest searchRequest) {
        BasePagingResponse<List<CareWelfareMerchantInfoDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareMerchant的请求参数不能为空");
            }

            @Override
            public void executeService() {
                CareWelfareMerchantInfoBSearcher careWelfareMerchantInfoSearch = CareWelfareMerchantInfoProviderConverter.convertRequestSO2Searcher(searchRequest);
                PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());
                careWelfareMerchantInfoSearch.setOutStatus(OutStatusEnum.IN.name());
                List<CareWelfareMerchantInfoDO> list = careWelfareMerchantInfoDao.search(careWelfareMerchantInfoSearch);
                PageInfo<CareWelfareMerchantInfoDO> pageInfo = new PageInfo<>(list);
                final List<CareWelfareMerchantInfoDTO> careWelfareMerchantInfoDTOList = CareWelfareMerchantInfoProviderConverter.convertResponseDOList2DTOList(list);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, careWelfareMerchantInfoDTOList, searchRequest, pageInfo.getTotal());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_SEARCH;
            }
        });
        return basePagingResponse;
    }

    @Override
    public BaseResponse<CareWelfareMerchantInfoDTO> getManagerDetail(BaseGetRequest baseGetRequest) {
        BaseResponse<CareWelfareMerchantInfoDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "查询CareWelfareMerchant的请求参数不能为空");
                BizCheckUtil.checkNotNull(baseGetRequest.getId(), "查询CareWelfareMerchant的ID请求参数不能为空");
            }

            @Override
            public void executeService() {
                CareWelfareMerchantInfoDO careWelfareMerchantInfoDO = careWelfareMerchantInfoDao.get((String) baseGetRequest.getId());
                CareWelfareMerchantInfoDTO careWelfareMerchantInfoDTO = CareWelfareMerchantInfoProviderConverter.convertResponseDO2DTO(careWelfareMerchantInfoDO);
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                CommonFavoriteDO commonFavoriteDO = commonFavoriteService.getBySoldierIdAndTypeAndBizId(currentUser.getSoldierId(),"MERCHANT",careWelfareMerchantInfoDTO.getId());
                careWelfareMerchantInfoDTO.setFavorite(commonFavoriteDO==null?false:true);
                careWelfareMerchantInfoDTO.setFavoriteId(commonFavoriteDO==null?null:commonFavoriteDO.getId());
                List<CareWelfareMerchantDiscountDO> list = careWelfareMerchantDiscountService.listByMerchantId((String) baseGetRequest.getId());
                careWelfareMerchantInfoDTO.setDiscounts(CareWelfareMerchantDiscountProviderConverter.convertResponseDOList2DTOList(list));
                baseResponse.setDto(careWelfareMerchantInfoDTO);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_GET;
            }

        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> managerUpdate(CareWelfareMerchantInfoUpdateRequest updateRequest) {
        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                OutStatusEnum outStatusEnum = OutStatusEnum.getByEnumName(updateRequest.getOutStatus());
                BizCheckUtil.checkNotNull(outStatusEnum, "修改的OutStatus请求参数不在范围内");
                BizCheckUtil.checkNotNull(updateRequest, "修改的CareWelfareMerchantInfo请求参数不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getId(), "修改CareWelfareMerchantInfo的请求参数ID不能为空");
            }

            @Override
            public void executeService() {
                updateRequest.setModifierId(CurrentUserUtil.getCurrentUser().getUserId());
                updateRequest.setModifierType(UserTypeEnum.GOV_EMPLOYEE.name());
//                CareWelfareMerchantInfoDO oldDo = careWelfareMerchantInfoDao.get(updateRequest.getId());
                CareWelfareMerchantInfoDO careWelfareMerchantInfoDO = CareWelfareMerchantInfoProviderConverter.convertRequestUO2DO(updateRequest);
                int affectRowNum = careWelfareMerchantInfoDao.update(careWelfareMerchantInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BasePagingResponse<List<CareWelfareMerchantInfoDTO>> outSearch(CareWelfareMerchantInfoSearchRequest searchRequest) {
        BasePagingResponse<List<CareWelfareMerchantInfoDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareMerchant的请求参数不能为空");
                BizCheckUtil.checkIsTrue("QXRZ,ZDTC".contains(searchRequest.getOutStatus()), "查询分页CareWelfareMerchant的outStatus不在范围");
            }

            @Override
            public void executeService() {
                CareWelfareMerchantInfoBSearcher careWelfareMerchantInfoSearch = CareWelfareMerchantInfoProviderConverter.convertRequestSO2Searcher(searchRequest);
                PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());
                List<CareWelfareMerchantInfoDO> list = careWelfareMerchantInfoDao.search(careWelfareMerchantInfoSearch);
                PageInfo<CareWelfareMerchantInfoDO> pageInfo = new PageInfo<>(list);

                final List<CareWelfareMerchantInfoDTO> careWelfareMerchantInfoDTOList = CareWelfareMerchantInfoProviderConverter.convertResponseDOList2DTOList(list);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, careWelfareMerchantInfoDTOList, searchRequest, pageInfo.getTotal());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_SEARCH;
            }
        });
        return basePagingResponse;
    }

    @Override
    public BaseResponse<CareWelfareMerchantInfoDTO> getOutDetail(BaseGetRequest baseGetRequest) {
        return getManagerDetail(baseGetRequest);
    }

    @Override
    public BaseResponse<Integer> outUpdate(CareWelfareMerchantInfoUpdateRequest updateRequest) {
        return null;
    }

    @Override
    public BaseResponse<Integer> cancelEnter(CareWelfareMerchantInfoUpdateRequest updateRequest) {
        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改的CareWelfareMerchantInfo请求参数不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getId(), "修改CareWelfareMerchantInfo的请求参数ID不能为空");
            }

            @Override
            public void executeService() {
                CareWelfareMerchantInfoDO oldDo = careWelfareMerchantInfoDao.get(updateRequest.getId());
                oldDo.setModifierId(CurrentUserUtil.getCurrentUser().getUserId());
                oldDo.setModifierType(UserTypeEnum.GOV_EMPLOYEE.name());
                oldDo.setOutStatus(OutStatusEnum.QXRZ.name());
                oldDo.setOutStatusRemark(updateRequest.getOutStatusRemark());
                oldDo.setOutTime(new Date());
                int affectRowNum = careWelfareMerchantInfoDao.update(oldDo);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BasePagingResponse<List<CareWelfareMerchantInfoDTO>> searchOutWait(CareWelfareMerchantInfoSearchRequest searchRequest) {
        BasePagingResponse<List<CareWelfareMerchantInfoDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareMerchant的请求参数不能为空");
                BizCheckUtil.checkIsTrue("OUT_WAIT,ZDTC".contains(searchRequest.getOutStatus()),"提交状态不在范围内！");
            }

            @Override
            public void executeService() {
                CareWelfareMerchantInfoBSearcher careWelfareMerchantInfoSearch = CareWelfareMerchantInfoProviderConverter.convertRequestSO2Searcher(searchRequest);
                PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());

                careWelfareMerchantInfoSearch.setOutStatus(searchRequest.getOutStatus());
                careWelfareMerchantInfoSearch.setOutTimeBgn(searchRequest.getApplyBegin());
                careWelfareMerchantInfoSearch.setOutTimeEnd(searchRequest.getApplyEnd());
                List<CareWelfareMerchantInfoDO> list = careWelfareMerchantInfoDao.search(careWelfareMerchantInfoSearch);
                PageInfo<CareWelfareMerchantInfoDO> pageInfo = new PageInfo<>(list);

                final List<CareWelfareMerchantInfoDTO> careWelfareMerchantInfoDTOList = CareWelfareMerchantInfoProviderConverter.convertResponseDOList2DTOList(list);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, careWelfareMerchantInfoDTOList, searchRequest, pageInfo.getTotal());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_SEARCH;
            }
        });
        return basePagingResponse;
    }

    @Override
    public BaseResponse<Integer> outAudit(CareWelfareMerchantInfoUpdateRequest updateRequest) {
        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改的CareWelfareMerchantInfo请求参数不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getId(), "修改CareWelfareMerchantInfo的请求参数ID不能为空");
            }

            @Override
            public void executeService() {
                CareWelfareMerchantInfoDO oldDo = careWelfareMerchantInfoDao.get(updateRequest.getId());
                BizCheckUtil.checkNotNull(oldDo,"未找到对应ID的数据");
                oldDo.setModifierId(CurrentUserUtil.getCurrentUser().getUserId());
                oldDo.setModifierType(UserTypeEnum.GOV_EMPLOYEE.name());
                oldDo.setOutStatus(OutStatusEnum.ZDTC.name());
                oldDo.setOutTime(new Date());
                oldDo.setRemark(updateRequest.getRemark());
                int affectRowNum = careWelfareMerchantInfoDao.update(oldDo);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public void exportApplyList(HttpServletResponse httpServletResponse, CareWelfareMerchantEntryApplySearchRequest searchRequest) {
        BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareMerchantEntryApply的请求参数不能为空");
        List<CareWelfareMerchantEntryApply> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(searchRequest.getIdList())) {
            CareWelfareMerchantEntryApplyBSearcher careWelfareMerchantEntryApplyBSearcher = CareWelfareMerchantEntryApplyProviderConverter.convertRequestSO2Searcher(searchRequest);
            PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());
            list = careWelfareMerchantEntryApplyDao.search(careWelfareMerchantEntryApplyBSearcher);
        } else {
            list = careWelfareMerchantEntryApplyDao.selectByIds(searchRequest.getIdList());
        }
        if (!CollectionUtils.isEmpty(list)) {

            final List<CareWelfareMerchantEntryApplyExportVO> careWelfareMerchantEntryApplyExportVOList = CareWelfareMerchantEntryApplyProviderConverter.convertResponseDOList2ExportVOList(list);
            careWelfareMerchantEntryApplyExportVOList.forEach(l->{
                l.setStatus(ProcessStatusEnum.getByEnumName(l.getStatus()).getDesc());
                l.setType(MerchantTypeEnum.getByEnumName(l.getType()).getDesc());
                l.setBusinessLicenseType(BusinessLicTypeEnum.getByEnumName(l.getBusinessLicenseType()).getDesc());
            });
            ExcelUtils.exportExcel(careWelfareMerchantEntryApplyExportVOList, CareWelfareMerchantEntryApplyExportVO.class,"申请数据","SHEET1",httpServletResponse);
        }
    }

    @Override
    public BaseResponse<Integer> managerPublish(CareWelfareMerchantInfoUpdateRequest updateRequest) {
        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改的CareWelfareMerchantInfo请求参数不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getPublished(), "修改CareWelfareMerchantInfo的请求参数published不能为空");
            }

            @Override
            public void executeService() {
                int affectRowNum = careWelfareMerchantInfoDao.publishList(updateRequest.getIdList(),updateRequest.getPublished());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> reApply(CareWelfareMerchantInfoUpdateRequest updateRequest) {
        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改的CareWelfareMerchantInfo请求参数不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getId(), "修改CareWelfareMerchantInfo的请求ID不能为空");
            }

            @Override
            public void executeService() {
                int affectRowNum = careWelfareMerchantInfoDao.reApply(updateRequest.getId(),updateRequest.getOutStatusRemark());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BasePagingResponse<List<CareWelfareMerchantInfoLogDTO>> getUpdateLog(CareWelfareMerchantInfoLogSearchRequest searchRequest) {
        BasePagingResponse<List<CareWelfareMerchantInfoLogDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareMerchant的请求参数不能为空");
                BizCheckUtil.checkNotNull(searchRequest.getId(),"ID不能为空");
            }

            @Override
            public void executeService() {
                PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());

                List<CareWelfareMerchantInfoLog> list = careWelfareMerchantInfoLogDao.getById(searchRequest.getId());
                PageInfo<CareWelfareMerchantInfoLog> pageInfo = new PageInfo<>(list);

                final List<CareWelfareMerchantInfoLogDTO> careWelfareMerchantInfoLogDTOList = CareWelfareMerchantInfoLogProviderConverter.convertResponseDOList2DTOList(list);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, careWelfareMerchantInfoLogDTOList, searchRequest, pageInfo.getTotal());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_SEARCH;
            }
        });
        return basePagingResponse;
    }

    @Override
    public BaseResponse<Integer> outApply(CareWelfareMerchantInfoUpdateRequest request) {
        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(request, "修改的CareWelfareMerchantInfoUpdateRequest请求参数不能为空");
                BizCheckUtil.checkNotBlank(request.getId(),"id不能为空");
                BizCheckUtil.checkNotBlank(request.getOutStatusRemark(),"退出原因不能为空");
            }

            @Override
            public void executeService() {
                CareWelfareMerchantInfoDO careWelfareMerchantInfoDO = careWelfareMerchantInfoDao.get(request.getId());
                careWelfareMerchantInfoDO.setOutStatusRemark(request.getOutStatusRemark());
                careWelfareMerchantInfoDO.setOutStatus(OutStatusEnum.OUT_WAIT.name());
                int affectRowNum = careWelfareMerchantInfoDao.update(careWelfareMerchantInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });

        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> updateCareWelfareMerchantDiscount(CareWelfareMerchantDiscountUpdateRequest request) {
        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(request, "新增的CareWelfareMerchantDiscount请求参数不能为空");
            }

            @Override
            @Transactional(rollbackFor = Exception.class)
            public void executeService() {
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                CareWelfareMerchantDiscountDO update = CareWelfareMerchantDiscountProviderConverter.convertRequestUO2DO(request);
                update.setModifyDatetime(new Date());
                update.setModifierId(currentUser.getUserId());
                update.setModifierType(currentUser.getUserType());
                int i = careWelfareMerchantDiscountDao.update(update);
                ResponseUtils.setupSuccessResponse(baseResponse, i);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });

        return baseResponse;
    }

    @Override
    public BaseResponse<String> insertCareWelfareMerchantDiscount(CareWelfareMerchantDiscountInsertRequest request) {
        BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(request, "新增的CareWelfareMerchantDiscount请求参数不能为空");
            }

            @Override
            @Transactional(rollbackFor = Exception.class)
            public void executeService() {
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                CareWelfareMerchantDiscountDO insert = CareWelfareMerchantDiscountProviderConverter.convertRequestIO2DO(request);
                insert.setCreateDatetime(new Date());
                insert.setCreatorId(currentUser.getUserId());
                insert.setCreatorType(currentUser.getUserType());
                insert.setModifyDatetime(new Date());
                insert.setModifierId(currentUser.getUserId());
                insert.setModifierType(currentUser.getUserType());
                careWelfareMerchantDiscountDao.insert(insert);
                ResponseUtils.setupSuccessResponse(baseResponse, insert.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });

        return baseResponse;
    }

    @Override
    public BaseResponse<CareWelfareMerchantDiscountDTO> getCareWelfareMerchantDiscount(BaseGetRequest baseGetRequest) {
        BaseResponse<CareWelfareMerchantDiscountDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {

            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "新增的CareWelfareMerchantDiscount请求参数不能为空");
            }

            @Override
            @Transactional(rollbackFor = Exception.class)
            public void executeService() {
                CareWelfareMerchantDiscountDO careWelfareMerchantDiscountDO = careWelfareMerchantDiscountDao.get((String)baseGetRequest.getId());
                CareWelfareMerchantDiscountDTO careWelfareMerchantDiscountDTO = CareWelfareMerchantDiscountProviderConverter.convertResponseDO2DTO(careWelfareMerchantDiscountDO);
                ResponseUtils.setupSuccessResponse(baseResponse, careWelfareMerchantDiscountDTO);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_UPDATE;
            }

        });

        return baseResponse;
    }

    @Override
    public BasePagingResponse<List<CareWelfareMerchantDiscountDTO>> getCareWelfareMerchantDiscountList(CareWelfareMerchantDiscountSearchRequest searchRequest) {
        BasePagingResponse<List<CareWelfareMerchantDiscountDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareMerchant的请求参数不能为空");
            }

            @Override
            public void executeService() {
                PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());

                List<CareWelfareMerchantDiscountDO> list = careWelfareMerchantDiscountDao.listByMerchantId(searchRequest.getMerchantId());
                PageInfo<CareWelfareMerchantDiscountDO> pageInfo = new PageInfo<>(list);

                final List<CareWelfareMerchantDiscountDTO> careWelfareMerchantDiscountDTOList = CareWelfareMerchantDiscountProviderConverter.convertResponseDOList2DTOList(list);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, careWelfareMerchantDiscountDTOList, searchRequest, pageInfo.getTotal());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_INFO_SEARCH;
            }
        });
        return basePagingResponse;
    }

}
